﻿#pragma once

#include <QObject>
#include <QSettings>
#include <QMap>
#include <QVariant>
#include <QJsonArray>
#include "VarDef.h"

namespace DBKey {
	struct Item;
}

class RamDB : public QObject
{
	Q_OBJECT
public:
	RamDB(QObject *parent=0);
	~RamDB();
	typedef DBKey::Item KEY;
	QString Get(const KEY& key,const QString& defVal=QString()) const;
	void Set(const KEY& key,const QString& value);
	bool GetBool(const KEY& key,bool devVal=false) const;
	void Set(const KEY& key,bool value);
	int GetInt(const KEY& key,int defVal=0) const;
	void Set(const KEY& key, int value);
	void Set(const KEY& key,const QJsonArray& val);
	QJsonArray GetJsonArray(const KEY& key,QJsonArray defVal=QJsonArray()) const;
	void SetVariant(const KEY& key,const QVariant& var);
	QVariant GetVariant(const KEY& key,const QVariant& defVal=QVariant()) const;
Q_SIGNALS:
	void DbSaved(const KEY& key,const QVariant& value);
private:
	void Load();
	void Save(const KEY& key, const QVariant& value);
private:
	QMap<QString, QVariant> mRamDB;
	QSettings mFileDB;
};
DECL_TYPE_NAME(RamDB)

